package com.wei.tang.ui.me;

import java.util.ArrayList;
import java.util.List;

import com.wei.tang.AppConest;
import com.wei.tang.AppContext;
import com.wei.tang.R;
import com.wei.tang.adapter.TalkInformationItemAdapter;
import com.wei.tang.adapter.UserBeanItemAdapter;
import com.wei.tang.base.BaseActivity;
import com.wei.tang.bean.CourseBean;
import com.wei.tang.bean.UserBean;
import com.wei.tang.db.UserDao;
import com.wei.tang.ui.course.CourseAcitivity;
import com.wei.tang.ui.me.RegistLoginActivity.LoginTask;
import com.wei.tang.util.Tlog;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import butterknife.InjectView;

public class MyStudentActivity extends BaseActivity {
	private Activity mySelf = this;

	@InjectView(R.id.lv_my_student_list)
	ListView lv_my_student_list;

	private List<UserBean> userBeans = new ArrayList<UserBean>();
	private UserBeanItemAdapter adapter = null;

	@Override
	public void onClick(View v) {
	}

	@Override
	public int getLayoutId() {
		return R.layout.my_student_activity;
	}

	@Override
	protected int getActionBarTitle() {
		return R.string.app_name;
	}

	@Override
	protected boolean hasBackButton() {
		return true;
	}

	@Override
	public void initData() {
		LoginTask loginTask = new LoginTask();
		loginTask.execute(0);
	}

	class LoginTask extends AsyncTask<Integer, Integer, Integer> {
		String msg = "";
		int code = AppConest.WS_SUCCESS;

		@Override
		protected Integer doInBackground(Integer... params) {
			UserDao userDao = new UserDao(mySelf);
			userBeans = userDao.findStuentsByName(AppContext.USERBEAN.getUser_name());
			if (userBeans == null || userBeans.size() == 0) {
				msg = "当前没有关联用户！";
				code = AppConest.WS_FALL;
			} else {
				msg = "查询成功";
				code = AppConest.WS_SUCCESS;
			}
			return code;
		}

		@Override
		protected void onPostExecute(Integer result) {
			hideWaitDialog();

			if (code == AppConest.WS_SUCCESS) {
				adapter = new UserBeanItemAdapter(mySelf, userBeans, R.layout.userbean_item);
				lv_my_student_list.setAdapter(adapter);
				adapter.notifyDataSetChanged();
			}
			AppContext.showToast(msg);
		}
	}

	@Override
	public void initView() {

		adapter = new UserBeanItemAdapter(mySelf, userBeans, R.layout.userbean_item);
		lv_my_student_list.setAdapter(adapter);
		lv_my_student_list.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				// Intent intent = new Intent(mySelf, CourseAcitivity.class);
				// mySelf.startActivity(intent);
			}
		});
	}
}
